package com.eastfair.builder.controller;

import com.eastfair.builder.dto.ConstructionPersonnelDTO;
import com.eastfair.builder.entity.ConstructionPersonnel;
import com.eastfair.builder.dto.ConstructionPersonnelSaveDTO;
import com.eastfair.builder.dto.ConstructionPersonnelUpdateDTO;
import com.eastfair.builder.dto.ConstructionPersonnelPageQuery;
import com.eastfair.builder.service.ConstructionPersonnelService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.builder.vo.ConstructionPersonnelVO;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.venueservice.dto.ViolationRecordDTO;
import com.eastfair.venueservice.vo.ViolationRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 * 前端控制器
 * 施工人员信息
 * </p>
 *
 * @author linan
 * @date 2023-05-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/constructionPersonnel")
@Api(value = "ConstructionPersonnel", tags = "施工人员信息")
@PreAuth(replace = "builder:constructionPersonnel:")
public class ConstructionPersonnelController extends SuperController<ConstructionPersonnelService, Long, ConstructionPersonnel, ConstructionPersonnelPageQuery, ConstructionPersonnelSaveDTO, ConstructionPersonnelUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ConstructionPersonnel> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ConstructionPersonnel> constructionPersonnelList = list.stream().map((map) -> {
            ConstructionPersonnel constructionPersonnel = ConstructionPersonnel.builder().build();
            //TODO 请在这里完成转换
            return constructionPersonnel;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(constructionPersonnelList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ConstructionPersonnelPageQuery> params) {
        ConstructionPersonnelPageQuery pageQuery = params.getModel();

    }

    /**
     * 添加人员
     *
     * @param constructionPersonnelDTO constructionPersonnelDTO
     * @return r
     */
    @ApiOperation(value = "添加人员")
    @PostMapping(value = "/addConstructionPersonnel")
    public R<Boolean> addConstructionPersonnel(@RequestBody ConstructionPersonnelDTO constructionPersonnelDTO) {
        log.info("addConstructionPersonnel - 添加人员, param={}", constructionPersonnelDTO);
        try {
            return R.success(baseService.addConstructionPersonnel(constructionPersonnelDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 人员列表
     *
     * @param constructionPersonnelDTO constructionPersonnelDTO
     * @return r
     */
    @ApiOperation(value = "人员列表")
    @PostMapping(value = "/queryConstructionPersonnel")
    public R<List<ConstructionPersonnelVO>> queryConstructionPersonnel(@RequestBody ConstructionPersonnelDTO constructionPersonnelDTO) {
        log.info("queryConstructionPersonnel - 人员列表, param={}", constructionPersonnelDTO);
        try {
            return R.success(baseService.queryConstructionPersonnel(constructionPersonnelDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 通过展位查人员列表
     *
     * @param constructionPersonnelDTO constructionPersonnelDTO
     * @return r
     */
    @ApiOperation(value = "通过展位查人员列表")
    @PostMapping(value = "/queryConstructionPersonnelBySiteName")
    public R<List<ConstructionPersonnelVO>> queryConstructionPersonnelBySiteName(@RequestBody ConstructionPersonnelDTO constructionPersonnelDTO) {
        log.info("queryConstructionPersonnel - 通过展位查人员列表, param={}", constructionPersonnelDTO);
        try {
            return R.success(baseService.queryConstructionPersonnelBySiteName(constructionPersonnelDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 通过展会查人员列表
     *
     * @param constructionPersonnelDTO constructionPersonnelDTO
     * @return r
     */
    @ApiOperation(value = "通过展会查人员列表")
    @PostMapping(value = "/queryConstructionPersonnelByExhibition")
    public R<List<ConstructionPersonnelVO>> queryConstructionPersonnelByExhibition(@RequestBody ConstructionPersonnelDTO constructionPersonnelDTO) {
        log.info("queryConstructionPersonnelByExhibition - 通过展会查人员列表, param={}", constructionPersonnelDTO);
        try {
            return R.success(baseService.queryConstructionPersonnelByExhibition(constructionPersonnelDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询人员信息是否在施工人员维护表中
     *
     * @param constructionPersonnelDTO constructionPersonnelDTO
     * @return r
     */
    @ApiOperation(value = "查询人员信息是否在施工人员维护表中")
    @PostMapping(value = "/checkConstructionPersonnel")
    public R<Boolean> checkConstructionPersonnel(@RequestBody ConstructionPersonnelDTO constructionPersonnelDTO) {
        log.info("checkConstructionPersonnel - 查询人员信息是否在施工人员维护表中, param={}", constructionPersonnelDTO);
        try {
            return R.success(baseService.checkConstructionPersonnel(constructionPersonnelDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 导入人员信息.
     *
     * @param file the multipart file
     * @return the r
     * @throws Exception the exception
     */
    @PostMapping("/importConstructionPersonnel")
    public R<Boolean> importConstructionPersonnel(@RequestPart("file") MultipartFile file, @NonNull @RequestParam("reportSpaceId") Long  reportSpaceId) throws Exception {
        log.info("importConstructionPersonnel - 导入人员信息");
        try {
            return R.successDef(baseService.importConstructionPersonnel(file,reportSpaceId));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

}
